package logic.ui.uieditscene
{
	import common.manager.ObserverEvt;
	
	import component.show.containers.UIFrame;
	import component.show.containers.tree.Tree;
	import component.show.containers.tree.TreeCellRendererVO;
	import component.show.containers.tree.TreeEvent;
	import component.show.control.UISprite;
	import component.untils.ComponentItem;
	
	import fl.containers.ScrollPane;
	
	import flash.events.MouseEvent;
	
	import logic.def.UIURL;
	import logic.ui.cell.ScanUITree_UITreeCell;

	public class ScanUITreeMed extends UISprite
	{ 
		public static const NAME : String = "ScanUITree";
		
		public static const CLICKUITREENODE : String = "ScanUITreeMed_ClickUITreeNode";
		
		private var m_fraTitle : UIFrame;
		private var m_treUI : Tree;
		private var m_scrUITree : ScrollPane;
		
		private var m_rootTreeVO : Array;
		
		public function ScanUITreeMed()
		{
			super(NAME,UIURL["ScanUITree"]);
			
			m_fraTitle = collection["FraTitle"];
			m_treUI = collection["TreUI"];
			m_scrUITree = collection["ScrUITree"];
			
			m_rootTreeVO = [];
			m_fraTitle.opaqueBackground = 0x669966;
			
			m_scrUITree.source = m_treUI;
			
			m_treUI.itemRenderer = ScanUITree_UITreeCell;
			m_treUI.treeWidth = 100;
			
			m_fraTitle.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
			m_fraTitle.addEventListener(MouseEvent.MOUSE_UP,onStopDrag);
			
			m_treUI.addEventListener(TreeEvent.CLICK_NODE,onClickUITreeNode);
		}
		
		public  override function destory() : void
		{
			m_fraTitle.removeEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
			m_fraTitle.removeEventListener(MouseEvent.MOUSE_UP,onStopDrag);
			
			m_treUI.removeEventListener(TreeEvent.CLICK_NODE,onClickUITreeNode);
			
			m_fraTitle = null;
			m_treUI = null;
		}
		
		private function onStartDrag(evt : MouseEvent) : void
		{
			this.startDrag();
		}
		
		private function onStopDrag(evt : MouseEvent) : void
		{
			this.stopDrag();
		}
		
		public function refreshTree(uiRootGroup:Vector.<ComponentItem>) : void
		{
			var treeVO : TreeCellRendererVO;
			
			m_rootTreeVO = [];
			
			for(var i:int=0;i<uiRootGroup.length;i++)
			{
				treeVO = new TreeCellRendererVO;
				
				treeVO.data = uiRootGroup[i];
				
				m_rootTreeVO.push(treeVO);
				
				if(uiRootGroup[i].childs.length > 0)
				{
					checkItem(treeVO,uiRootGroup[i]);
				}
			}
			
			m_treUI.dataProvider = m_rootTreeVO;
			
			m_treUI.width = 200;
			m_scrUITree.update();
//			m_treUI.height = uiRootGroup.length * uiRootGroup[0].item.height+15;
		}
		
		private function checkItem(pTreeVO:TreeCellRendererVO,comItem : ComponentItem) : void
		{
			var treeVO : TreeCellRendererVO;
			
			for(var i:int=0;i<comItem.childs.length;i++)
			{
				treeVO = new TreeCellRendererVO;
				
				var childItem : ComponentItem = comItem.childs[i];
				
				treeVO.data = childItem;
				treeVO.parentNode = pTreeVO;
				
				if(childItem.childs.length > 0)
				{
					checkItem(treeVO,childItem);
				}
			}
			m_treUI.update();
		}
		
		private function onClickUITreeNode(evt : TreeEvent) : void
		{
			ObserverEvt.Send(CLICKUITREENODE,evt.item.data.data);
			
			m_scrUITree.update();
		}
	}
}